package com.chen.authority.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.MultiResponse;
import com.chen.authority.common.response.PageResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.response.SingleResponse;
import com.chen.authority.common.util.ExcelUtil;
import com.chen.authority.common.util.WrapperUtil;
import com.chen.authority.model.dto.DictDataDto;
import com.chen.authority.model.qo.DictDataQo;
import com.chen.authority.common.model.PageQo;
import com.chen.authority.model.vo.DictDataVo;
import com.chen.authority.service.DictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 字典数据管理
 *
 * @author chengy
 * @date 2023-09-04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/dict-data")
@Api(tags = "字典数据管理 DictDataController")
public class DictDataController {

    private final DictDataService dictDataService;

    @GetMapping("/list")
    @ApiOperation("查询字典数据列表")
    @SaCheckPermission("system:dict:list")
    public PageResponse<DictDataVo> page(PageQo pageQo, DictDataQo dictDataQo) {
        return PageResponse.success(dictDataService.pageQueryDictDataList(pageQo, dictDataQo));
    }

    @GetMapping("/{id}")
    @ApiOperation("查询字典数据详细信息")
    @SaCheckPermission("system:dict:update")
    public SingleResponse<DictDataVo> get(@PathVariable Integer id) {
        return SingleResponse.success(dictDataService.getById(id).toVo(DictDataVo.class));
    }

    @GetMapping
    @ApiOperation("根据字典类型查询字典数据")
    @SaCheckPermission("system:dict:list")
    public MultiResponse<DictDataVo> list(DictDataQo qo) {
        return MultiResponse.success(dictDataService.findDictDataList(WrapperUtil.buildQueryWrapper(qo)));
    }

    @PostMapping
    @ApiOperation("新增字典数据")
    @SaCheckPermission("system:dict:add")
    @Log(module = "系统管理-字典管理", businessType = BusinessType.INSERT)
    public Response add(@Valid @RequestBody DictDataDto dto) {
        return dictDataService.addOrUpdateDictData(null, dto);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新字典数据")
    @SaCheckPermission("system:dict:update")
    @Log(module = "系统管理-字典管理", businessType = BusinessType.UPDATE)
    public Response update(@PathVariable Integer id, @Valid @RequestBody DictDataDto dto) {
        return dictDataService.addOrUpdateDictData(id, dto);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("删除字典数据")
    @SaCheckPermission("system:dict:delete")
    @Log(module = "系统管理-字典管理", businessType = BusinessType.DELETE)
    public Response delete(@PathVariable Integer[] ids) {
        return dictDataService.removeByIds(Arrays.asList(ids)) ? Response.success() : Response.fail();
    }

    @PostMapping("/export")
    @ApiOperation("导出字典数据")
    @SaCheckPermission("system:dict:export")
    @Log(module = "系统管理-字典管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, DictDataQo qo) {
        List<DictDataVo> dictDatas = dictDataService.findDictDataList(WrapperUtil.buildQueryWrapper(qo));
        ExcelUtil.exportExcel(response, "字典数据表", DictDataVo.class, dictDatas);
    }

}